package org.jeecg.modules.basic.warehouseGroup.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.basic.warehouseGroup.entity.WmsBasicWarehouseGroup;
import org.jeecg.modules.basic.warehouseGroup.entity.WmsBasicWarehouseGroupUser;
import org.jeecg.modules.basic.warehouseGroup.entity.WmsBasicWarehouseGroupWarehouse;
import org.jeecg.modules.basic.warehouseGroup.mapper.WmsBasicWarehouseGroupUserMapper;
import org.jeecg.modules.basic.warehouseGroup.mapper.WmsBasicWarehouseGroupWarehouseMapper;
import org.jeecg.modules.basic.warehouseGroup.mapper.WmsBasicWarehouseGroupMapper;
import org.jeecg.modules.basic.warehouseGroup.service.IWmsBasicWarehouseGroupService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @Description: 仓库组
 * @Author: jeecg-boot
 * @Date:   2023-08-02
 * @Version: V1.0
 */
@Service
public class WmsBasicWarehouseGroupServiceImpl extends ServiceImpl<WmsBasicWarehouseGroupMapper, WmsBasicWarehouseGroup> implements IWmsBasicWarehouseGroupService {

	@Autowired
	private WmsBasicWarehouseGroupMapper wmsBasicWarehouseGroupMapper;
	@Autowired
	private WmsBasicWarehouseGroupUserMapper wmsBasicWarehouseGroupUserMapper;
	@Autowired
	private WmsBasicWarehouseGroupWarehouseMapper wmsBasicWarehouseGroupWarehouseMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(WmsBasicWarehouseGroup wmsBasicWarehouseGroup, List<WmsBasicWarehouseGroupUser> wmsBasicWarehouseGroupUserList,List<WmsBasicWarehouseGroupWarehouse> wmsBasicWarehouseGroupWarehouseList) {
		wmsBasicWarehouseGroupMapper.insert(wmsBasicWarehouseGroup);
		if(wmsBasicWarehouseGroupUserList!=null && wmsBasicWarehouseGroupUserList.size()>0) {
			for(WmsBasicWarehouseGroupUser entity:wmsBasicWarehouseGroupUserList) {
				//外键设置
				entity.setWmsBasicWarehouseGroupId(wmsBasicWarehouseGroup.getId());
				wmsBasicWarehouseGroupUserMapper.insert(entity);
			}
		}
		if(wmsBasicWarehouseGroupWarehouseList!=null && wmsBasicWarehouseGroupWarehouseList.size()>0) {
			for(WmsBasicWarehouseGroupWarehouse entity:wmsBasicWarehouseGroupWarehouseList) {
				//外键设置
				entity.setWmsBasicWarehouseGroupId(wmsBasicWarehouseGroup.getId());
				wmsBasicWarehouseGroupWarehouseMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(WmsBasicWarehouseGroup wmsBasicWarehouseGroup,List<WmsBasicWarehouseGroupUser> wmsBasicWarehouseGroupUserList,List<WmsBasicWarehouseGroupWarehouse> wmsBasicWarehouseGroupWarehouseList) {
		wmsBasicWarehouseGroupMapper.updateById(wmsBasicWarehouseGroup);
		
		//1.先删除子表数据
		wmsBasicWarehouseGroupUserMapper.deleteByMainId(wmsBasicWarehouseGroup.getId());
		wmsBasicWarehouseGroupWarehouseMapper.deleteByMainId(wmsBasicWarehouseGroup.getId());
		
		//2.子表数据重新插入
		if(wmsBasicWarehouseGroupUserList!=null && wmsBasicWarehouseGroupUserList.size()>0) {
			for(WmsBasicWarehouseGroupUser entity:wmsBasicWarehouseGroupUserList) {
				//外键设置
				entity.setId(null);
				entity.setWmsBasicWarehouseGroupId(wmsBasicWarehouseGroup.getId());
				Long count = wmsBasicWarehouseGroupUserMapper.selectCount(new LambdaQueryWrapper<WmsBasicWarehouseGroupUser>()
						.eq(WmsBasicWarehouseGroupUser::getUserId, entity.getUserId()));
				if (count > 0) {
					throw new JeecgBootException("一个仓管员只能绑定一个仓库组！");
				}
				wmsBasicWarehouseGroupUserMapper.insert(entity);
			}
		}
		if(wmsBasicWarehouseGroupWarehouseList!=null && wmsBasicWarehouseGroupWarehouseList.size()>0) {
			for(WmsBasicWarehouseGroupWarehouse entity:wmsBasicWarehouseGroupWarehouseList) {
				//外键设置
				entity.setId(null);
				entity.setWmsBasicWarehouseGroupId(wmsBasicWarehouseGroup.getId());
				wmsBasicWarehouseGroupWarehouseMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		wmsBasicWarehouseGroupUserMapper.deleteByMainId(id);
		wmsBasicWarehouseGroupWarehouseMapper.deleteByMainId(id);
		wmsBasicWarehouseGroupMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			wmsBasicWarehouseGroupUserMapper.deleteByMainId(id.toString());
			wmsBasicWarehouseGroupWarehouseMapper.deleteByMainId(id.toString());
			wmsBasicWarehouseGroupMapper.deleteById(id);
		}
	}

	@Override
	public List<String> getManageWarehouseId(String userId) {
		List<String> list = new ArrayList<>();
		if (StrUtil.isBlank(userId)){
			return list;
		}
		List<WmsBasicWarehouseGroupUser> groupList = wmsBasicWarehouseGroupUserMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseGroupUser>()
				.eq(WmsBasicWarehouseGroupUser::getUserId, userId)
		);
		if (CollectionUtil.isEmpty(groupList)){
			return list;
		}
		List<String> groupIdList = groupList.stream().map(WmsBasicWarehouseGroupUser::getWmsBasicWarehouseGroupId).collect(Collectors.toList());
		if (CollectionUtil.isEmpty(groupIdList)){
			return list;
		}
		List<WmsBasicWarehouseGroupWarehouse> warehouses = wmsBasicWarehouseGroupWarehouseMapper.selectList(new LambdaQueryWrapper<WmsBasicWarehouseGroupWarehouse>()
				.in(WmsBasicWarehouseGroupWarehouse::getWmsBasicWarehouseGroupId, groupIdList)
		);
		return warehouses.stream().map(WmsBasicWarehouseGroupWarehouse::getWmsBasicWarehouseId).distinct().collect(Collectors.toList());
	}

}
